package com.lwt.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lwt.yygh.cmn.client.DictFeignClient;
import com.lwt.yygh.hosp.repository.HospitalRepository;
import com.lwt.yygh.hosp.service.HospitalService;
import com.lwt.yygh.model.hosp.Hospital;
import com.lwt.yygh.vo.hosp.HospitalQueryVo;
import com.lwt.yygh.vo.hosp.HospitalSetQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Weton Li
 * @Date 2021/4/21 16:02
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

// 远程调用接口注入
    @Autowired
    private DictFeignClient dictFeignClient;
    @Override
    public void save(Map<String, Object> paramMap) {
        // 将数据添加到mongoDb数据库，第一次为添加，第二次为更新

        // 将map集合转为为对象
        String mapString = JSONObject.toJSONString(paramMap);// 将map集合变为字符串
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class); // 将字符串转换为对象
        // 判断是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExist = hospitalRepository.getHospitalByHoscode(hoscode);

        if (hospitalExist != null) {
            // 更新医院
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date()); // 修改时间时赋值新的当前时间
            hospital.setIsDeleted(hospitalExist.getIsDeleted());
            hospitalRepository.save(hospital);
        }else{
            // 添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date()); // 不存在医院则创建时赋值当前新时间
            hospital.setUpdateTime(new Date()); // 修改时间时赋值新的当前时间
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);

        }
    }

    /**
     * 通过医院编号获取一条医院信息
     * @param hoscode
     * @return
     */
    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    /**
     * 查询医院信息，分页显示
     * 后将简单分页重新注入其他模块查询出的医院等级信息 远程调用来的信息添加到分页数据中
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Pageable pageable = PageRequest.of(page-1,limit); // 创建分页对象
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true); // 创建条件匹配器
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        Example<Hospital> example = Example.of(hospital,exampleMatcher); // 创建查询对象
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

//        List<Hospital> content = pages.getContent();
// 使用java8 Stream流 mongo中hostype
        pages.getContent().stream().forEach(item->{
            this.setHospitalHosType(item); // 向遍历出来的每条分页信息中再设置医院等级信息名
        });

        return pages;
    }
    // 调用数据字典中接口封装好的方法，传入dictCode和value值查询医院，放进map集合中
    private Hospital setHospitalHosType(Hospital hospital) {
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());

        // 练习，查询省 市 地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString", hostypeString);
        return hospital;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        // 根据id查询医院信息
        Hospital hospital = hospitalRepository.findById(id).get();
        // 设置修改的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    // 查询MongoDB数据的一条医院详细信息
    @Override
    public Map<String, Object> getHospById(String id) {
        HashMap<String, Object> map = new HashMap<>();
        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        map.put("hospital",hospital);
        map.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null); // 不需要重复返回，因为已经在map中封装过
        return map;
    }

    /**
     * ScheduleServiceImpl调用HospitalServiceImpl中的医院区域方法
     * @param hoscode
     * @return
     */
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (hospital != null) {
            return hospital.getHosname();
        }
        return null;
    }
}
